## setup

# clean environment
rm(list=ls())

# load packages
library(ellipsis)
library(ggplot2)
library(tidyverse)

# attrition package from A. Coppock required:
library(devtools)
  # devtools::install_github("acoppock/attrition")
library(attrition)

# load data
dat <- read.csv2("data_experiment_outcomes_csv.csv",
                 encoding = 'UTF-8')

## assign correct types (just to be safe)
# outcome variable to numeric
dat$response <- as.numeric(as.character(dat$response))

# IVs to numeric
dat$immback <- as.numeric(as.character(dat$immback))
dat$partisan <- as.numeric(as.character(dat$partisan))


## estimate bounds

# create variable indicating whether individual was treated (all those that do not have NA value in outcome)
dat$treated <- !is.na(dat$response)

# extreme value bounds for the effect of the "turk" treatment
bounds_turk<-estimator_ev(Y=response, Z=immback, R=treated, minY=0, maxY=1, alpha=0.05, data=dat)

# extreme value bounds for the effect of the "convinced voter" treatment
bounds_convinced<-estimator_ev(Y=response, Z=partisan, R=treated, minY=0, maxY=1, alpha=0.05, data=dat)


## create plot

# prepare data for plot
bounds_df <- data.frame(rbind(bounds_turk[3:4], bounds_convinced[3:4]),
                        rbind(bounds_turk[1:2], bounds_convinced[1:2]))
bounds_df$treat <- c("Immigrant\nBackground", "Partisan")
bounds_df$treat <- factor(bounds_df$treat, levels = rev(c("Immigrant\nBackground", "Partisan")))

# plot
ggplot(data=bounds_df, aes(x=treat)) + 
  geom_errorbar(aes(ymin=low_est, ymax=upp_est), width=0, size=2) +
  geom_errorbar(aes(ymin=ci_lower, ymax=ci_upper), width=.2) +
  xlab("Treatment") + ylab("Treatment Effect") +
  coord_flip() +
  geom_hline(yintercept=0, linetype="dashed") +
  theme_bw() +
  theme(panel.grid = element_blank())
